/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.hiraeth.modules.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hiraeth.common.utils.PageParams;
import com.hiraeth.modules.sys.dao.SysDictDao;
import com.hiraeth.modules.sys.entity.SysDictEntity;
import com.hiraeth.modules.sys.service.SysDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {

    @Override
    public IPage<SysDictEntity> queryPage(PageParams<SysDictEntity> pageParams) {
        SysDictEntity query = pageParams.mapToObject(SysDictEntity.class);
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ObjectUtils.isNotNull(query.getParentId()), SysDictEntity::getParentId, query.getParentId())
                .like(ObjectUtils.isNotEmpty(query.getName()), SysDictEntity::getName, query.getName());

        return baseMapper.selectPage(pageParams, queryWrapper);
    }

    /**
     * 根据字典id递归删除字典
     * @param ids 字典id，多个id使用英文逗号隔开
     */
    @Override
    @Transactional
    public void removeRecursive(Long[] ids) {
        String idsStr = StrUtil.join(",", ids);
        baseMapper.deleteDictRecursiveById(idsStr);
    }

}
